package com.sos.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sos.dto.OrderSearch;
import com.sos.entity.po.Order;
import com.sos.entity.po.OrderDetail;
import com.sos.mapper.OrderMapper;
import com.sos.service.OrderService;
import com.sos.util.TokenUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 港某人
 * @date 2021/3/20 13:24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Object> implements OrderService {

    @Override
    public Page<Order> getOrderByDoctorId(OrderSearch orderSearch) {
        Page<Order> page = new Page<>(orderSearch.getPage(), orderSearch.getLimit());
        String doctorId = TokenUtil.getDoctorId();
        if (doctorId != null) {
            orderSearch.setDoctorId(doctorId);
            List<Order> order = baseMapper.getOrderByDoctorId(page, orderSearch);
            page.setRecords(order);
        }
        return page;
    }

    @Override
    public OrderDetail getOrderById(OrderSearch orderSearch) {
        return baseMapper.getOrderById(orderSearch.getObjId());
    }

    @Override
    public Page<OrderDetail> getOrderDetailByDoctorId(OrderSearch orderSearch) {
        Page<OrderDetail> page = new Page<>(orderSearch.getPage(), orderSearch.getLimit());
        String doctorId = TokenUtil.getDoctorId();
        if (doctorId != null) {
            orderSearch.setDoctorId(doctorId);
            List<OrderDetail> detail = baseMapper.getOrderDetailByDoctorId(page, orderSearch);
            page.setRecords(detail);
        }
        return page;
    }

    @Override
    public String verifyOrder(String status, String[] orderIds) {
        if (orderIds != null) {
            for (String orderId : orderIds) {
                Order order = new Order();
                order.setObjId(orderId);
                order.setStatus(status);
                baseMapper.updateOrder(order);
            }
        }
        return "success";
    }

    @Override
    public void updateOrderDetail(OrderDetail orderDetail) {
        baseMapper.updateOrderDetail(orderDetail);
    }

}
